﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
namespace WPFExcelReport.ViweModels
{
    public class ITXMENUDEFViewModel:BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        private Dictionary<string, object> dicParamater
        {
            get;
            set;
        }
        public string Mandant
        {
            get
            {
                return app.Mandant;
            }
        }
        public string company
        {
            get
            {
                return app.company;
            }
        }
        public SETINTEXDB SetEnv
        {
            get;
            set;

        }
        public SETINTEXDB SetDetail
        {
            get;
            set;
        }
         public SETINTEXDB SetMaster
        {
            get;
            set;
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public ITXMENUDEFViewModel()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetMaster == null)
            {
                SetMaster = new SETINTEXDB();
            }
            if (SetDetail == null)
            {
                SetDetail = new SETINTEXDB();
            }    
        }
        public DataSet LoadMenu(string client = "00")
        { 
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string filePath = "";
                string fileroot = Path.Combine(PathHelper.ApplicationPath, @"Documents");
                string[] files = System.IO.Directory.GetFiles(fileroot);

                foreach (string file in files)
                {
                    if (!file.Contains("MENU"))
                        continue;
                    if (System.IO.File.Exists(file))
                    {
                        setResult = new DataSet();
                        setResult.ReadXml(file);
                        if (setResult.Tables.Count > 0)
                        {
                            setResult.Tables[0].TableName = "ICC_MENUDEF";
                        }
                        setReturn.Merge(setResult);
                    }

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
            return setReturn;
        }
        bool IsSetEnvUpdate = true;
        public DataSet Load(string client, string company)
        { 
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwData = null;
            try
            {
                if (IsSetEnvUpdate)
                {
                    SetEnv.Clear();
                    SetEnv.Merge(LoadEnv());
                }
                IsSetEnvUpdate = false;
                dwData = SetEnv.ICC_MENUDEF.NewRow();
                dwData["MANDANT"] = client;
                dwData["COMPANY"] = company;
                setResult = INTEXDBC.QueryICC_MENUDEF(dwData, 9999);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadEnv()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                if (INTEXDBC == null)
                {
                    return setReturn;
                }
                DataRow dwData = SetEnv.ICC_FUNCTIONS.NewRow();
                dwData["MANDANT"] = Mandant;
                setResult = INTEXDBC.QueryICC_FUNCTIONS(dwData, 9999);
                /*foreach (DataRow row in setResult.Tables[0].Rows)
                {
                    row["DESCRIPTION"] = Convert8859P1ToGB2312(row["DESCRIPTION"].ToString());
                }*/
                setReturn.Merge(setResult);
                dwData = SetEnv.HRSTAMM.NewRow();
                dwData["HRMANDANT"] = Mandant;
                setResult = INTEXDBC.QueryHRSTAMM(dwData, 4);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void Save(DataSet src, string client)
        {
            try
            {


                string userid = "";
                string processed = "";
                string company = "";
                foreach (DataRow rw in src.Tables["ICC_MENUDEF"].Rows)
                {
                    /*userid = rw["USERID"].ToString();
                    userid = Helpers.Helper.Left(userid + "                 ", 8);
                    if (!processed.Contains(userid + ";"))
                    {
                        processed += userid + ";";                    
                        INTEXDBC.DeleteICC_MENUDEF(client, company, userid);
                    }*/
                    rw["MANDANT"] = client;
                    //rw["HEADER"] = gb2312.ConvertBack(rw["HEADER"].ToString(), typeof(string), null, null);
                    company = rw["COMPANY"].ToString();
                    INTEXDBC.SaveICC_MENUDEF(rw);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Delete()
        {
            try
            {
                INTEXDBC.DeleteICC_MENUDEF(null, 1);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
